let arr = [];
function drawBezier({ ctx, bezierCtrlNodesArr, color }) {
	arr = bezierCtrlNodesArr;
	var nodeArr = bezierCtrlNodesArr,
		bezierArr = [];
	if(nodeArr.length === 2) {
		console.warn('Control nodes should be more then two!')
		var startNode = nodeArr[0],
			endNode = nodeArr[1]
		ctx.moveTo(startNode.x, startNode.y)
		ctx.lineTo(endNode.x, endNode.y)
		ctx.strokeStyle = color
		ctx.stroke()
	} else if(nodeArr.length === 3) {
		var startNode = nodeArr[0],
			ctrlNode = nodeArr[1],
			endNode = nodeArr[2]
		ctx.beginPath()
		ctx.moveTo(startNode.x, startNode.y)
		ctx.quadraticCurveTo(ctrlNode.x, ctrlNode.y, endNode.x, endNode.y)
		ctx.strokeStyle = color
		ctx.stroke()
	} else if(nodeArr.length === 4) {
		var startNode = nodeArr[0],
			ctrlNodeA = nodeArr[1],
			ctrlNodeB = nodeArr[2],
			endNode = nodeArr[3]
		ctx.beginPath()
		ctx.moveTo(startNode.x, startNode.y)
		ctx.bezierCurveTo(ctrlNodeA.x, ctrlNodeA.y, ctrlNodeB.x, ctrlNodeB.y, endNode.x, endNode.y)
		ctx.strokeStyle = color
		ctx.stroke()
	} else {
		for(var i = 0; i < 1; i+=0.01) {
			bezierArr.push(bezier(i));
		}
		console.log( bezierArr );
		bezierArr.forEach(function(obj, index) {
			if (index) {
				var startX = bezierArr[index - 1].x,
					startY = bezierArr[index - 1].y,
					x = obj.x,
					y = obj.y
				ctx.beginPath()
				ctx.moveTo(startX, startY)
				ctx.lineTo(x, y)
				ctx.strokeStyle = color
				ctx.stroke()
			}
		})
	}
}

function bezier(t) {
	var x = 0,
		y = 0,
		n = arr.length - 1;
	arr.map(function(item, index) {
		if(!index) {
			x += item.x * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
			y += item.y * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
		} else {
			x += factorial(n) / factorial(index) / factorial(n - index) * item.x * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
			y += factorial(n) / factorial(index) / factorial(n - index) * item.y * Math.pow(( 1 - t ), n - index) * Math.pow(t, index) 
		}
	})
	return {
		x: x,
		y: y
	}
}

function factorial(num) {
	if (num <= 1) {
		return 1;
	} else {
		return num * factorial(num - 1);
	}
}